package com.imooc.jms.producer.impl;

import com.imooc.jms.producer.ProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import javax.annotation.Resource;
import javax.jms.*;

public class ProducerServiceImpl implements ProduceService {

//    private static final Logger logger = LoggerFactory.getLogger(ProducerServiceImpl.class);
    @Autowired
    private JmsTemplate jmsTemplate;
    //    这个name是与producer.xml里的bean id 一样的
//    这个是队列模式
    @Resource(name = "queueDestination")
//    这个是订阅模式
//    @Resource(name = "topicDestination")
    private Destination destination;

    @Override
    public void sendMessage(final String message) {
        //使用JmsTemplate 发送消息
        jmsTemplate.send(destination, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage(message);
                return textMessage;
            }
        });
//        logger.info("这里是消息{}", message);
        System.out.println("这里是消息{===>}" + message);
    }
}
